package demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class UpdateSubject {
    public static void main(String[] args) {
        {
            //类内部把自己注册了一下
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                return;
            }
            //连接字符串
            String url = "jdbc:mysql://localhost:3306/java68?useSSL=false&useUnicode=true&characterEncoding=UTF-8";
            Connection con = null;
            Statement st = null;
            //新增一门课程
            Scanner in=new Scanner(System.in);
            System.out.println("输入要修改的课程号:");
            long id=in.nextLong();
            System.out.println("请输入上面的课程的新信息,新名称,课时以及所属年级号:");
            String newName=in.next();
            int hour=in.nextInt();
            long newGradeid=in.nextLong();

            try {
                con = DriverManager.getConnection(url, "root", "root1234");
                System.out.println(con.getClass().getName());//接口实现类
                st = con.createStatement();
                String sql="update s_subject set subjectname='"+newName+"' ,classhour="+hour+",gradeid="+newGradeid+" where id="+id;
                System.out.println(sql);

                int r=st.executeUpdate(sql);
                System.out.println("更新了"+r+"行");
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (st != null)
                        st.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    if (con != null)
                        con.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
